package com.ck.listen.csm;

import com.alibaba.fastjson.JSON;
import com.ck.listen.bean.CanalMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


/**
 * @author ck
 * @date 2024/6/26 15:44\
 * desc:canal消费者
 */

@Component
@Slf4j
public class CanalConsumer {

    @RabbitListener(queues = "canal")
    public void canalQueueConsumer(Map<String, Object> message) {
        log.info("监听到canal数据: {}表的变更信息:{}",message.get("table"), message);
        if (message == null || message.isEmpty()) {
            log.info("canal数据为空");
            return;
        }
        CanalMessage canalMessage = JSON.parseObject(JSON.toJSONString(message), CanalMessage.class);
        List data = canalMessage.getData();

        // TODO 根据业务需求处理canal数据
        // 将监听到的数据写入redis或同步到es等......
    }
}
